package com.zhenyi.youqu.admin.index;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.youqu.common.model.YqCategory;
import com.youqu.common.model.YqShop;
import com.youqu.common.model.YqShopUser;
import com.zhenyi.core.interceptor.ReferenceInterceptor;

public class CategoryController extends YQAdminController {

	public void list() {
		YqShopUser user = getShopUser();
		Kv kv = Kv.create();
		if (!user.getRole().equals(YqShopUser.ROLE_SUPER_ADMIN)) {
			kv.set("appId", user.getAppId());
		}
		if (user.getRole().equals(YqShopUser.ROLE_SHOP)) {
			kv.set("shopId", user.getShopId());
		}
		SqlPara sqlPara = db.getSqlPara("YQAdminCategories", kv);
		Page<YqCategory> pages = YqCategory.dao.paginate(getPage(), getPageSize(), sqlPara);
		setRainbow(pages);
		setItems(pages.getList());
		render("/yq/admin/category/list.html");
	}

	@Before(ReferenceInterceptor.class)
	public void info() {
		Long id = getParaToLong("id");
		YqShopUser shopUser = getShopUser();
		YqCategory info;
		if (id != null) {
			info = YqCategory.dao.findById(id);
			if (info == null) {
				renderError(404);
				return;
			}
		} else {
			info = new YqCategory();
			if (shopUser.getRole().equals(YqShopUser.ROLE_SHOP)) {
				info.setShopId(shopUser.getShopId());
			}
		}
		SqlPara sqlPara = db.getSqlPara("YQShops", Kv.by("appId", shopUser.getAppId()));
		List<YqShop> shops = YqShop.dao.find(sqlPara);
		setData("shops", shops);
		setInfoItem(JsonKit.toJson(info));
		render("/yq/admin/category/info.html");
	}

	public void submit() {
		Long id = getParaToLong("id");
		String title = getPara("title", "");
		String subtitle = getPara("subtitle");
		Integer status = getParaToInt("status", 1);
		Long sort = getParaToLong("sort", 99999L);
		String cover = getPara("cover");
		String image = getPara("image");
		Long shopId = getParaToLong("shopId");

		YqCategory info = null;
		if (id == null) {
			info = new YqCategory();
			info.setTitle("");
			info.setStatus(1);
			info.setSort(99999L);
			info.save();
		} else {
			info = YqCategory.dao.findById(id);
			if (info == null) {
				renderError(404);
				return;
			}
		}
		info.setTitle(title);
		info.setSubtitle(subtitle);
		info.setStatus(status);
		info.setSort(sort);
		info.setCover(cover);
		info.setImage(image);
		info.setShopId(shopId);

		YqShopUser shopUser = getShopUser();

		if (shopId == null) {
			info.setShopId(shopUser.getShopId());
		}
		if (shopUser.isSuperAdmin()) {
			info.setIsGlobal(1);
		}
		renderFlag(info.update());
	}

	public void delete() {
		Long id = getParaToLong("id");
		if (id == null) {
			renderFailure();
			return;
		}
		YqCategory info = YqCategory.dao.findById(id);
		if (info == null) {
			renderError(404);
			return;
		}
		renderFlag(info.delete());
	}

	public void updateIsTop() {
		Long id = getParaToLong("id");
		Integer isTop = getParaToInt("isTop", 1);
		if (id == null) {
			renderFailure(500, "category id");
			return;
		}
		YqCategory info = YqCategory.dao.findById(id);
		if (info == null) {
			renderError(404);
			return;
		}
		SqlPara sp = db.getSqlPara("YQAdminCategories");
		List<YqCategory> list = YqCategory.dao.find(sp);
		for (YqCategory cat : list) {
			cat.setIsTop(0);
			cat.update();
		}
		info.setIsTop(isTop);
		renderFlag(info.update());
	}

}
